<div class="content-section introduction">
    <div class="feature-intro">
        <h1>Table <span>Edit</span></h1>
        <span>Cell and Row editing provides a rapid and user friendly way to manipulate data.</span>
    </div>
    <app-demoActions github="table" stackblitz="tableedit-demo"></app-demoActions>
</div>

<div class="content-section implementation p-fluid">
    <p-toast></p-toast>

    <div class="card">
        <h5>Cell Editing</h5>
        <p-table [value]="products1" dataKey="id" responsiveLayout="scroll">
            <ng-template pTemplate="header">
                <tr>
                    <th>Code</th>
                    <th>Name</th>
                    <th>Category</th>
                    <th>Price</th>
                </tr>
            </ng-template>
            <ng-template pTemplate="body" let-product>
                <tr>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="product.code">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{product.code}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="product.name" required>
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{product.name}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText [(ngModel)]="product.inventoryStatus">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{product.inventoryStatus}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td pEditableColumn>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="product.price">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{product.price | currency: 'USD'}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                </tr>
            </ng-template>
        </p-table>
    </div>

    <div class="card">
    <h5>Row Editing</h5>
        <p-table [value]="products2" dataKey="id" editMode="row" responsiveLayout="scroll">
            <ng-template pTemplate="header">
                <tr>
                    <th>Code</th>
                    <th>Name</th>
                    <th>Category</th>
                    <th>Price</th>
                    <th style="width:8rem"></th>
                </tr>
            </ng-template>
            <ng-template pTemplate="body" let-product let-editing="editing" let-ri="rowIndex">
                <tr [pEditableRow]="product">
                    <td>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="product.code">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{product.code}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="product.name" required>
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{product.name}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <p-dropdown [options]="statuses" appendTo="body" [(ngModel)]="product.inventoryStatus" [style]="{'width':'100%'}"></p-dropdown>
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{product.inventoryStatus}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td>
                        <p-cellEditor>
                            <ng-template pTemplate="input">
                                <input pInputText type="text" [(ngModel)]="product.price">
                            </ng-template>
                            <ng-template pTemplate="output">
                                {{product.price | currency: 'USD'}}
                            </ng-template>
                        </p-cellEditor>
                    </td>
                    <td style="text-align:center">
                        <button *ngIf="!editing" pButton pRipple type="button" pInitEditableRow icon="pi pi-pencil" (click)="onRowEditInit(product)" class="p-button-rounded p-button-text"></button>
                        <button *ngIf="editing" pButton pRipple type="button" pSaveEditableRow icon="pi pi-check" (click)="onRowEditSave(product)" class="p-button-rounded p-button-text p-button-success mr-2"></button>
                        <button *ngIf="editing" pButton pRipple type="button" pCancelEditableRow icon="pi pi-times" (click)="onRowEditCancel(product, ri)" class="p-button-rounded p-button-text p-button-danger"></button>
                    </td>
                </tr>
            </ng-template>
        </p-table>
    </div>
</div>

<div class="content-section documentation">
    <p-tabView>
        <p-tabPanel header="Source">
            <a href="https://github.com/primefaces/primeng/tree/master/src/app/showcase/components/table/" class="btn-viewsource" target="_blank">
                <span>View on GitHub</span>
            </a>
            <a href="https://stackblitz.com/edit/primeng-tableedit-demo" class="btn-viewsource" style="margin-left: .5em;" target="_blank">
                <span>Edit in StackBlitz</span>
            </a>

<app-code lang="markup" ngNonBindable ngPreserveWhitespaces>
&lt;p-toast&gt;&lt;/p-toast&gt;

&lt;div class="card"&gt;
    &lt;h5&gt;Cell Editing&lt;/h5&gt;
    &lt;p-table [value]="products1" dataKey="id" responsiveLayout="scroll"&gt;
        &lt;ng-template pTemplate="header"&gt;
            &lt;tr&gt;
                &lt;th&gt;Code&lt;/th&gt;
                &lt;th&gt;Name&lt;/th&gt;
                &lt;th&gt;Category&lt;/th&gt;
                &lt;th&gt;Quantity&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/ng-template&gt;
        &lt;ng-template pTemplate="body" let-product&gt;
            &lt;tr&gt;
                &lt;td pEditableColumn&gt;
                    &lt;p-cellEditor&gt;
                        &lt;ng-template pTemplate="input"&gt;
                            &lt;input pInputText type="text" [(ngModel)]="product.code"&gt;
                        &lt;/ng-template&gt;
                        &lt;ng-template pTemplate="output"&gt;
                            &#123;&#123;product.code&#125;&#125;
                        &lt;/ng-template&gt;
                    &lt;/p-cellEditor&gt;
                &lt;/td&gt;
                &lt;td pEditableColumn&gt;
                    &lt;p-cellEditor&gt;
                        &lt;ng-template pTemplate="input"&gt;
                            &lt;input pInputText type="text" [(ngModel)]="product.name" required&gt;
                        &lt;/ng-template&gt;
                        &lt;ng-template pTemplate="output"&gt;
                            &#123;&#123;product.name&#125;&#125;
                        &lt;/ng-template&gt;
                    &lt;/p-cellEditor&gt;
                &lt;/td&gt;
                &lt;td pEditableColumn&gt;
                    &lt;p-cellEditor&gt;
                        &lt;ng-template pTemplate="input"&gt;
                            &lt;input pInputText [(ngModel)]="product.inventoryStatus"&gt;
                        &lt;/ng-template&gt;
                        &lt;ng-template pTemplate="output"&gt;
                            &#123;&#123;product.inventoryStatus&#125;&#125;
                        &lt;/ng-template&gt;
                    &lt;/p-cellEditor&gt;
                &lt;/td&gt;
                &lt;td pEditableColumn&gt;
                    &lt;p-cellEditor&gt;
                        &lt;ng-template pTemplate="input"&gt;
                            &lt;input pInputText type="text" [(ngModel)]="product.price"&gt;
                        &lt;/ng-template&gt;
                        &lt;ng-template pTemplate="output"&gt;
                            &#123;&#123;product.price | currency: 'USD'&#125;&#125;
                        &lt;/ng-template&gt;
                    &lt;/p-cellEditor&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/ng-template&gt;
    &lt;/p-table&gt;
&lt;/div&gt;

&lt;div class="card"&gt;
&lt;h5&gt;Row Editing&lt;/h5&gt;
    &lt;p-table [value]="products2" dataKey="id" editMode="row" responsiveLayout="scroll"&gt;
        &lt;ng-template pTemplate="header"&gt;
            &lt;tr&gt;
                &lt;th&gt;Code&lt;/th&gt;
                &lt;th&gt;Name&lt;/th&gt;
                &lt;th&gt;Category&lt;/th&gt;
                &lt;th&gt;Quantity&lt;/th&gt;
                &lt;th style="width:8rem"&gt;&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/ng-template&gt;
        &lt;ng-template pTemplate="body" let-product let-editing="editing" let-ri="rowIndex"&gt;
            &lt;tr [pEditableRow]="product"&gt;
                &lt;td&gt;
                    &lt;p-cellEditor&gt;
                        &lt;ng-template pTemplate="input"&gt;
                            &lt;input pInputText type="text" [(ngModel)]="product.code"&gt;
                        &lt;/ng-template&gt;
                        &lt;ng-template pTemplate="output"&gt;
                            &#123;&#123;product.code&#125;&#125;
                        &lt;/ng-template&gt;
                    &lt;/p-cellEditor&gt;
                &lt;/td&gt;
                &lt;td&gt;
                    &lt;p-cellEditor&gt;
                        &lt;ng-template pTemplate="input"&gt;
                            &lt;input pInputText type="text" [(ngModel)]="product.name" required&gt;
                        &lt;/ng-template&gt;
                        &lt;ng-template pTemplate="output"&gt;
                            &#123;&#123;product.name&#125;&#125;
                        &lt;/ng-template&gt;
                    &lt;/p-cellEditor&gt;
                &lt;/td&gt;
                &lt;td&gt;
                    &lt;p-cellEditor&gt;
                        &lt;ng-template pTemplate="input"&gt;
                            &lt;p-dropdown [options]="statuses" appendTo="body" [(ngModel)]="product.inventoryStatus" [style]="&#123;'width':'100%'&#125;"&gt;&lt;/p-dropdown&gt;
                        &lt;/ng-template&gt;
                        &lt;ng-template pTemplate="output"&gt;
                            &#123;&#123;product.inventoryStatus&#125;&#125;
                        &lt;/ng-template&gt;
                    &lt;/p-cellEditor&gt;
                &lt;/td&gt;
                &lt;td&gt;
                    &lt;p-cellEditor&gt;
                        &lt;ng-template pTemplate="input"&gt;
                            &lt;input pInputText type="text" [(ngModel)]="product.price"&gt;
                        &lt;/ng-template&gt;
                        &lt;ng-template pTemplate="output"&gt;
                            &#123;&#123;product.price | currency: 'USD'&#125;&#125;
                        &lt;/ng-template&gt;
                    &lt;/p-cellEditor&gt;
                &lt;/td&gt;
                &lt;td style="text-align:center"&gt;
                    &lt;button *ngIf="!editing" pButton pRipple type="button" pInitEditableRow icon="pi pi-pencil" (click)="onRowEditInit(product)" class="p-button-rounded p-button-text"&gt;&lt;/button&gt;
                    &lt;button *ngIf="editing" pButton pRipple type="button" pSaveEditableRow icon="pi pi-check" (click)="onRowEditSave(product)" class="p-button-rounded p-button-text p-button-success mr-2"&gt;&lt;/button&gt;
                    &lt;button *ngIf="editing" pButton pRipple type="button" pCancelEditableRow icon="pi pi-times" (click)="onRowEditCancel(product, ri)" class="p-button-rounded p-button-text p-button-danger"&gt;&lt;/button&gt;
                &lt;/td&gt;
            &lt;/tr&gt;
        &lt;/ng-template&gt;
    &lt;/p-table&gt;
&lt;/div&gt;
</app-code>

<app-code lang="typescript" ngNonBindable ngPreserveWhitespaces>
import &#123; Component, OnInit &#125; from '@angular/core';
import &#123; Product &#125; from '../../domain/product';
import &#123; ProductService &#125; from '../../service/productservice';
import &#123; SelectItem &#125; from 'primeng/api';
import &#123; MessageService &#125; from 'primeng/api';

@Component(&#123;
    templateUrl: './tableeditdemo.html',
    providers: [MessageService],
    styles: [`
        :host ::ng-deep .p-cell-editing &#123;
            padding-top: 0 !important;
            padding-bottom: 0 !important;
        &#125;
    `]
&#125;)
export class TableEditDemo implements OnInit &#123;

    products1: Product[];

    products2: Product[];

    statuses: SelectItem[];

    clonedProducts: &#123; [s: string]: Product; &#125; = &#123;&#125;;

    constructor(private productService: ProductService, private messageService: MessageService) &#123; &#125;

    ngOnInit() &#123;
        this.productService.getProductsSmall().then(data =&gt; this.products1 = data);
        this.productService.getProductsSmall().then(data =&gt; this.products2 = data);

        this.statuses = [&#123;label: 'In Stock', value: 'INSTOCK'&#125;,&#123;label: 'Low Stock', value: 'LOWSTOCK'&#125;,&#123;label: 'Out of Stock', value: 'OUTOFSTOCK'&#125;]
    &#125;

    onRowEditInit(product: Product) &#123;
        this.clonedProducts[product.id] = &#123;...product&#125;;
    &#125;

    onRowEditSave(product: Product) &#123;
        if (product.price &gt; 0) &#123;
            delete this.clonedProducts[product.id];
            this.messageService.add(&#123;severity:'success', summary: 'Success', detail:'Product is updated'&#125;);
        &#125;
        else &#123;
            this.messageService.add(&#123;severity:'error', summary: 'Error', detail:'Invalid Price'&#125;);
        &#125;
    &#125;

    onRowEditCancel(product: Product, index: number) &#123;
        this.products2[index] = this.clonedProducts[product.id];
        delete this.clonedProducts[product.id];
    &#125;

&#125;
</app-code>

        </p-tabPanel>

        <p-tabPanel header="StackBlitz">
            <ng-template pTemplate="content">
                <iframe src="https://stackblitz.com/edit/primeng-tableedit-demo?embed=1" style="width: 100%; height: 768px; border: none;"></iframe>
            </ng-template>
        </p-tabPanel>
    </p-tabView>
</div>
